package com.example.controller;


import com.alibaba.dubbo.config.annotation.Reference;


import com.example.domain.message.MessageConstant;
import com.example.domain.message.RedisConstant;
import com.example.domain.pojo.CheckGroup;
import com.example.domain.pojo.Setmeal;
import com.example.domain.query.QueryPageBean;
import com.example.domain.result.PageResult;
import com.example.domain.result.Result;
import com.example.domain.utils.QiniuUtils;
import com.example.service.CheckGroupService;
import com.example.service.SetMealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import javax.crypto.MacSpi;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    //注入checkGroupService
   @Autowired
    private CheckGroupService checkGroupService;


   @Autowired
    private SetMealService setMealService;

    //注入redis连接池
    @Autowired
    private JedisPool jedisPool;


    //查询所有的检查组信息
    @RequestMapping("/findAllCheckGroup")
    public Result findAllCheckGroup(){
        try {
            List<CheckGroup> allGroup = checkGroupService.findAllGroup();
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,allGroup);
        } catch (Exception e) {
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }

    }

    //接受文件上传 但是不存在,用户上传了图片,结果提交了取消按钮
    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile")MultipartFile imgFile){
                //上传的图片 imgFile MultipartFile
        try {
            //获取原始文件名称
            String originalFilename = imgFile.getOriginalFilename();
            //返回指定字符在此字符串中最后一次出现处的索引
            int i = originalFilename.lastIndexOf(".");
            //获取文件后缀名
            String sufffix = originalFilename.substring(i - 1);
            //使用UUID随机产生文件名称,防止同文件重名
            UUID uuid = UUID.randomUUID();
            //拼接新的名字
            String name=uuid.toString()+sufffix;
            //将文件上传
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),name);
            //返回的结果
            Result result= new Result(true,MessageConstant.PIC_UPLOAD_SUCCESS,name);

             //将上传的图片存入Redis,基于Redis的set集合存储
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,name);

            return result;

        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        }

    }

    //添加
    @RequestMapping("/add")
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds){
        try {
            setMealService.add(checkgroupIds,setmeal);
            return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
    }


    //分页查询数据
    @RequestMapping("/pageQuery")
    public PageResult add(@RequestBody QueryPageBean queryPageBean){
            PageResult pageResult = setMealService.queryPage(queryPageBean);
            return pageResult;
    }

    //delsetMeal删除
    //删除检查组
    @RequestMapping("/delsetMeal")
    public Result deletesetMeal(Integer id){
        try {
            setMealService.deletesetMeal(id);
            return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }

    }

    //编辑 edit
    @RequestMapping("/edit")
    public Result editCheckGroup(@RequestBody Setmeal setmeal, Integer[] checkgroupIds){

        try {
            //1:调用方法编辑修改检查组基本信息
            setMealService.editMeal(setmeal);
            //2:修改中间表
            setMealService.editMealAndGroup(setmeal.getId(),checkgroupIds);
            return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
//            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }


    //查询当前套餐选中的检查组
    //编辑 edit
    @RequestMapping("/findGroupIdByMealId")
    public Integer[] findGroupIdByMealId(Integer setMealId){
           Integer[] ids= setMealService.findGroupIdByMealId(setMealId);
           return ids;
    }


    //查询基本的信息
    @RequestMapping("/findMealById")
    public Setmeal findsetMealById(Integer setMealId){
        Setmeal setmeal=setMealService.findsetMealById(setMealId);
        return setmeal;
    }

}
